require.config({
	paths:{
		"jquery":"jquery",
		"common":"xz-common",
		"cookie":"jquery.cookie",
		"fly":"jquery.fly"
	}
})

require(["jquery","common","cookie"],function($){
    $(function(){
		//是否登录
        if($.cookie("login")){
        	$("#xz-login").text($.cookie("login")+" ，点击重新登录");
        }else{
        	$("#xz-login").text("【登录】");
        }
     //退出
      $("#xz-login").mousedown(function(){
      	$.removeCookie("login",{expires:0,path:"/"});
      	location.href = "../html/login.html";
      })
	})
	$(function(){
		$pot=1;	
	if(!$.cookie("goods")){
		$(".xz-point").css("display","block");
	}else{
     $(".xz-point").css("display","none");
	}
	var goods = converStrToObj($.cookie("goods"))
	//退出提示
	$(".xz-shopcar-Prompt span").mousedown(function(){
		$(".xz-shopcar-Prompt ").css("display","none")
	})
	//添加购物车内容
	var sumnow = 0;
	var sum = 0;
     for(val in goods){
     	$(".xz-clearshopcar").before(
     		'<tr  class="xz-goodsmessage" id="'+val +'">'
     		                  +'<td width="386" style="text-align:left">'
	         				   +'<a href="#" class="xz-goodsimage"><img src='
	         				   +goods[val].imgsrc+' /></a>'
	         				    +' <a href="#" class="productnames" style="color:#666";>'+goods[val].name+'</a></td>'
	         				    +'<td width="130">'+goods[val].pirce+'</td>'
	         					+'<td width="80">'+goods[val].integral+'</td>'
	         					+'<td width="130"><span class="shopcarleft">-</span>'
	         					+'<input type="text" class="shopcar-goodsnum" value="'
	         					+goods[val].num+'" />'
	         					+'<span class="shopcarright">+</span></td>'
	         					+'<td width="130" class="goodintegral">'+goods[val].integral*goods[val].num+ '</td>'
	         					+'<td width="130" class="smallsum">￥'+goods[val].integral*goods[val].num+'</td>'
	         					+'<td width="130"><span class="xz-delegoods"></span></td></tr>'
     	)
     	sumnow+=parseInt(goods[val].integral*goods[val].num);
     	sum +=parseInt(goods[val].num)
     }
     $(".xz-buygoods-sum3").text("￥"+sumnow);
      $(".xz-buygoods-sum2").text(sumnow);
      $(".xz-buygoods-sum1").text(sum);
     
        //左右按钮的增减功能	
        //减少
		$(".shopcarleft").mousedown(function(){
			var id = $(this).parents(".xz-goodsmessage").attr("id");
			 if(goods[id].num<=1){
			 	goods[id].num=1;
			 }else{
			 	goods[id].num--;
			 }
			 $(this).siblings(".shopcar-goodsnum").val(goods[id].num);
			 $(this).parent().siblings(".goodintegral").text(goods[id].integral*goods[id].num);
			 $(this).parent().siblings(".smallsum").text("￥"+goods[id].integral*goods[id].num);
			 $.cookie("goods",convertObjTostr(goods),{expires: 7,path: "/"})
			 var sum1 = 0 ;
			 var sumnow1 = 0;
			 for(val in goods){
			 	sum1 += parseInt(goods[val].integral*goods[val].num);
			 	sumnow1 += parseInt(goods[val].num);
			 }
			  $(".xz-buygoods-sum3").text("￥"+sum1);
			  $(".xz-buygoods-sum2").text(sum1);
			  $(".xz-buygoods-sum1").text(sumnow1);
		})
	//增加
     		$(".shopcarright").mousedown(function(){
			var id = $(this).parents(".xz-goodsmessage").attr("id");

			 	goods[id].num++;
			
			 $(this).siblings(".shopcar-goodsnum").val(goods[id].num);
			 $(this).parent().siblings(".goodintegral").text(goods[id].integral*goods[id].num);
			 $(this).parent().siblings(".smallsum").text("￥"+goods[id].integral*goods[id].num);
			 $.cookie("goods",convertObjTostr(goods),{expires: 7,path: "/"})
			 var sum2 = 0 ;
			 var sumnow2 = 0;
			 for(val in goods){
			 	sum2 += parseInt(goods[val].integral*goods[val].num);
			 	sumnow2 += parseInt(goods[val].num);
			 }
			  $(".xz-buygoods-sum3").text("￥"+sum2);
			  $(".xz-buygoods-sum2").text(sum2);
			  $(".xz-buygoods-sum1").text(sumnow2);
		})
     //删除商品
     $(".xz-delegoods").mousedown(function(){
     	var id =$(this).parents(".xz-goodsmessage").attr("id");
     	$(this).parents(".xz-goodsmessage").remove();
     	delete goods[id];
     	$.cookie("goods",convertObjTostr(goods),{expires:7,path:"/"})
     })
      //清空购物车
     $(".xz-clearall").mousedown(function(){

     	 		$(".xz-goodsmessage").remove();
     	 		console.log(1);
                $.removeCookie("goods",{expires:0,path:"/"});
                $(".xz-point").css("display","block");
     	 })
     
     
    })


		
		
		
	//cookie工具
   	function converStrToObj(str){
   		var obj = {};
				if(!str){
					return {};
				}
   		var arr = str.split(":");
   		for(var i = 0 ;i<arr.length;i++){  
   		  arr2=arr[i].split(",");
   		  obj[arr2[0]]={
   		  	name:arr2[1],
   		  	pirce:arr2[2],
            num:arr2[3],
   		  	integral:arr2[4],
   		  	imgsrc:arr2[5]
   		  	
   		  }
   		}
   		return obj;
   	}
   	//对象转字符串
   	function convertObjTostr(obj){
   		var str = "";
   		for(val in obj){
   		if(str){
   			str +=":";
   		 }
   		str +=val+","+obj[val].name+","+obj[val].pirce+","+obj[val].num+","
   		+obj[val].integral+","+obj[val].imgsrc
   		}
   		return str;
   	}
   	
   	$(function(){

	//购物车跳转
    $("#xz-shopcar").mousedown(function(){

    	location.href ="../html/shopcar.html";
    	
    });
    //注册页跳转
    $("#toresgister").mousedown(function(){
    	location.href = "../html/register.html";
    
    });
    //登录页跳转
     $("#xz-login").mousedown(function(){
    	location.href = "../html/login.html";
    	
    })
     //列表页跳转
     $(".xz-listofgoods-one a").mousedown(function(){
     	location.href = "../html/listgoods.html";
     })
	 $(".xz-continue-shopping a" ).mousedown(function(){
	 	location.href = "../html/listgoods.html"
	 })
   	})
   	
	
})
